package com.itheima.controller;

import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.utils.DateUtils;
import org.apache.dubbo.config.annotation.Reference;

import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileInputStream;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @PackageName: com.itheima.controller
 * @ClassName: Me
 * @Description: TODO
 * @Author: zzy
 * @DateTime: 2020/11/4 10:40
 * @Version 1.0
 */


@RestController
@RequestMapping("/backend_member")
public class BackendMemberController {
    @Reference
    private MemberService memberService;



    //查询所有会员
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return memberService.findPage(queryPageBean);
    }

    //添加会员
    @RequestMapping("/add")
    public Result add(@RequestBody Member member){

        try {
            memberService.add(member);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_MEMBER_FAIL);
        }
        return new Result(true,MessageConstant.ADD_MEMBER_SUCCESS);

    }
    @RequestMapping("/getIds")
    public Result getIds(HttpServletRequest request, @RequestBody List<Integer> list) {

        HttpSession session = request.getSession();
        session.setAttribute("ids",list);
        return new Result(true, "欢乐斗帝组");
    }

    @RequestMapping("/exportExcel")
    public Result exportBusinessReport(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        List<Integer> list = (List) session.getAttribute("ids");
        int i = 1;
        try {
            String temlateRealPath = request.getSession().getServletContext().getRealPath("template")
                    + File.separator + "exportMemberReport.xlsx";
            XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(new File(temlateRealPath)));
            XSSFSheet sheet = workbook.getSheetAt(0);
            for (Integer id : list) {
                Member member = memberService.findById(id);
                //获取每行数据
                String fileNumber = member.getFileNumber();
                String name = member.getName();
                String sex = member.getSex();
                if ("1".equals(sex)){
                    sex="男";
                }else if ("2".equals(sex)){
                    sex="女";
                }else {
                    sex="未知";
                }
                int age=0;
                Date birthday = member.getBirthday();
                if (birthday != null) {
                    Date now = new Date();
                    age = now.getYear() - birthday.getYear();
                }
                String manager = member.getManager();
                String regTime = DateUtils.parseDate2String(member.getRegTime());
                String phoneNumber = member.getPhoneNumber();

                sheet.createRow(i);
                XSSFRow row = sheet.getRow(i);
                i++;
                for (int j = 0; j < 7; j++) {
                    row.createCell(j);
                }
                row.getCell(0).setCellValue(fileNumber);
                row.getCell(1).setCellValue(name);
                row.getCell(2).setCellValue(sex);
                row.getCell(3).setCellValue(age);
                row.getCell(4).setCellValue(manager);
                row.getCell(5).setCellValue(regTime);
                row.getCell(6).setCellValue(phoneNumber);
            }
            ServletOutputStream out = response.getOutputStream();
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("content-Disposition", "attachment;filename=member.xlsx");
            workbook.write(out);
            out.flush();
            out.close();
            workbook.close();
            return null;
        } catch (Exception e) {

            return new Result(false, MessageConstant.GET_BUSINESS_REPORT_FAIL, null);
        }

    }
//删除会员
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            memberService.delete(id);
        }catch (Exception e){
            /*String message = e.getMessage();
            e.printStackTrace();*/
            return new Result(false, MessageConstant.DELETE_MEMBER_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_MEMBER_SUCCESS);

    }
    //根据会员id进行查找
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Member member=memberService.findById(id);
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,member);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }

    }


    //编辑会员
    @RequestMapping("/edit")
    public Result edit(@RequestBody Member member){
        try {
            memberService.edit(member);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_MEMBER_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_MEMBER_SUCCESS);
    }


    @RequestMapping("/names")
    public Result getManagerNames() {

        List<Map<String, Object>> list = memberService.findManagerByRoleId();

        return new Result(true, "检查信息成功", list);
    }



}
